package com.geovis.emergency.spd.biz.pc.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.geovis.common.auth.util.AuthUtil;
import com.geovis.emergency.spd.biz.pc.mapper.PcRollCallMapper;
import com.geovis.emergency.spd.biz.pc.mapper.PcRollCallPersonMapper;
import com.geovis.emergency.spd.biz.pc.service.IPcRollCallPersonService;
import com.geovis.emergency.spd.cache.SysCache;
import com.geovis.emergency.spd.entity.mobile.pojo.dto.RollCallDTO;
import com.geovis.emergency.spd.entity.pc.entity.PcRollCall;
import com.geovis.emergency.spd.entity.pc.entity.PcRollCallPerson;
import com.geovis.emergency.spd.entity.pc.pojo.constant.RollCallConstant;
import com.geovis.emergency.spd.entity.pc.pojo.enums.RollCallStatusEnum;
import com.geovis.emergency.spd.redis.RedisUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 应急预案指挥模块-点名名单 服务实现类
 * </p>
 *
 * @author lmolong
 * @since 2022-06-20
*/

@Service
@RequiredArgsConstructor
@Slf4j
public class PcRollCallPersonServiceImpl extends ServiceImpl<PcRollCallPersonMapper, PcRollCallPerson> implements IPcRollCallPersonService {

    private final PcRollCallMapper rollCalMapper;
    @Override
    public void updateSign(String pcRollCallId, String userId) {
        PcRollCall pcRollCall = rollCalMapper.selectById(pcRollCallId);
        Assert.notNull(pcRollCall, "点名不存在");
        Assert.isFalse(RollCallStatusEnum.CONFIRMED.getCode().equals(pcRollCall.getStatus()), "点名已经结束了");
        update(Wrappers.lambdaUpdate(PcRollCallPerson.class)
                .eq(PcRollCallPerson::getPcRollCallId,pcRollCallId)
                .eq(PcRollCallPerson::getPersonId,userId)
                .set(PcRollCallPerson::getStatus, RollCallStatusEnum.CONFIRMED.getCode())
        );
    }

    @Override
    public RollCallDTO notCheckList(String drillId) {
        LambdaUpdateWrapper<PcRollCallPerson> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(PcRollCallPerson::getPcDrillId,drillId);
        wrapper.eq(PcRollCallPerson::getPersonId, AuthUtil.getCurrentUserId());
        wrapper.eq(PcRollCallPerson::getStatus, RollCallStatusEnum.UNCONFIRMED.getCode());
        wrapper.inSql(PcRollCallPerson::getPcRollCallId, StrUtil.format("select id from pc_roll_call where is_deleted = '0' and status ='1' and pc_drill_id = '{}' order by create_time desc limit 1",drillId));
        List<PcRollCallPerson> list = baseMapper.selectList(wrapper);
        if(CollectionUtil.isNotEmpty(list)){
            RollCallDTO dto = new RollCallDTO();
            dto.setPcDrillId(list.get(0).getPcDrillId());
            dto.setUserId(list.get(0).getPersonId());
            dto.setPcRollCallId(list.get(0).getPcRollCallId());
            String paramValue = SysCache.getParamValue(RollCallConstant.CALL_ROLL_TIME_PARAM);
            int rollCallTime = RollCallConstant.DEFAULT_COUNT_DOWN;
            if (StrUtil.isNotBlank(paramValue)){
                rollCallTime = Convert.toInt(paramValue,rollCallTime / 1000) * 1000;
            }
            rollCallTime = RedisUtil.get(StrUtil.format(RollCallConstant.CALL_ROLL_TIME, dto.getPcRollCallId()),rollCallTime/1000) * 1000;
            dto.setCallRollTime(rollCallTime);
            return dto;
        }
        return null;
    }
}
